package com.snopy.concurrency.demo_threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname AbortPolicyTest
 * @description
 * @email 77912204@qq.com
 * @date 2022/02/19 21:10
 */
@Slf4j
public class RejectPolicyTest {
    //static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,2,0, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(5),new ThreadPoolExecutor.AbortPolicy());
    static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,5,0, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(3),new ThreadPoolExecutor.CallerRunsPolicy());
    //static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,5,0, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(3),new ThreadPoolExecutor.DiscardOldestPolicy());
    //static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,3,0, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(2),new ThreadPoolExecutor.DiscardPolicy());
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
                CodeFarmer codeFarmer = new CodeFarmer("工人先锋"+i+"号");

                tpe.execute(codeFarmer);
                log.info("工人先锋{}号,核心线程数->{},最大线程数->{},缓存队列->{},干活线程数->{},线程池大小->{}",i,tpe.getCorePoolSize(),tpe.getMaximumPoolSize(),tpe.getQueue().size(),tpe.getActiveCount(),tpe.getPoolSize());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
    static class CodeFarmer implements Runnable{
        String name;
        CodeFarmer(String name){
            this.name = name;
        }
        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(100);
                log.info("{} 干完活了~",name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /*while (true){

            }*/
        }
    }
}
